<template>
  <div class="food-audit">
    <!--    头部搜索-->
    <div class="header-search">
      <div class="header-left">
        <el-form :model="searchForm" inline ref="searchRef">
          <el-form-item label="菜品名称" prop="name">
            <el-input
              v-model="searchForm.name"
              size="small"
              placeholder="请输入菜品名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="食堂" prop="canteen">
            <el-select
              v-model="searchForm.canteen"
              placeholder="请选择食堂"
              size="small"
            >
              <el-option
                v-for="item in canteenOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="编号" prop="number">
            <el-input
              v-model="searchForm.number"
              size="small"
              placeholder="请输入编号"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-search"
              @click="search"
              >搜索</el-button
            >
            <el-button size="small" icon="el-icon-refresh-left" @click="reset"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="header-right">
        <el-button size="small" type="primary" @click="MultipleAudit">
          审核</el-button
        >
      </div>
    </div>
    <!--    表格区域-->
    <div class="table" style="height: calc(100% - 160px); overflow-y: auto">
      <el-table
        :header-cell-style="{ background: '#FEF6F6' }"
        ref="multipleTable"
        :data="foodTableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="number" label="编号" />
        <el-table-column prop="name" label="名称" />
        <el-table-column label="图片">
          <template #default="scope">
            <el-image
              :src="scope.row.images"
              style="width: 100px; height: 60px"
            >
              <template #error>
                <div class="image-slot">
                  <img
                    src="https://www.mingxiaobang.cn/upload/admin/food_1.jpg"
                    style="width: 100px; height: 60px"
                  />
                </div>
              </template>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column sortable prop="commentCount" label="评论数" />
        <el-table-column sortable prop="clickCount" label="点赞数" />
        <el-table-column prop="canteen" label="所属食堂">
          <template #default="scope">
            <span>{{ scope.row.canteen.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型">
          <template #default="scope">
            <el-tag size="small">{{
              scope.row.type === 1 ? "荤菜" : "素菜"
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              icon="el-icon-view"
              type="text"
              size="small"
              @click="audit(scope.row.id)"
              >审核</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      style="float: right; margin: 20px"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
      :current-page="pageNum"
      :page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      background
    >
    </el-pagination>
    <!-- 菜品批量审核对话框 -->
    <el-dialog
      v-model="foodAuditDialogVisible"
      title="菜品批量审核"
      width="30%"
    >
      <el-input
        v-model="foodAuditContent"
        placeholder="请输入审核意见"
        type="textarea"
      ></el-input>
      <template #footer>
        <span class="dialog-footer">
          <el-button size="small" type="primary" @click="submitAuit"
            >确定</el-button
          >
          <el-button size="small" type="danger" @click="rejectAudit"
            >驳回</el-button
          >
          <el-button size="small" @click="foodAuditDialogVisible = false"
            >取消</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { onMounted, ref } from "vue";
import { getCanteenList } from "../../../../api/canteen/menu-managerApi";
import commonPageRepository from "../../../../composables/common-page-repository";
import { useRouter } from "vue-router";
import { foodAudit, foodMultipleSubmit } from "../../../../api/canteen/audit";
import { ElMessage } from "element-plus";
export default {
  name: "FoodAudit",
  setup() {
    const searchForm = ref({});
    const canteenOptions = ref([]);
    //获取食堂列表
    function getCanteen() {
      getCanteenList().then((res) => {
        canteenOptions.value = res.data;
      });
    }
    const pageNum = ref(1);
    const pageSize = ref(50);
    const totalNum = ref("");
    const searchRef = ref(null);
    const foodTableData = ref([]);
    //获取菜品审核列表数据
    function getAuditFoodList() {
      foodAudit({
        pageNum: pageNum.value,
        pageSize: pageSize.value,
      }).then((res) => {
        foodTableData.value = res.data.content;
        totalNum.value = res.data.totalElements;
      });
    }
    const { pageSizeChange, pageCurrentChange } = commonPageRepository(
      pageSize,
      pageNum,
      getAuditFoodList
    );
    const router = useRouter();
    function audit(id) {
      router.push({
        name: "auditDetail",
        query: {
          id,
        },
      });
    }
    // 搜索功能
    function search() {
      pageNum.value = 1;
      foodAudit({
        pageNum: pageNum.value,
        pageSize: pageSize.value,
        canteenId: searchForm.value.canteen,
        name: searchForm.value.name,
        number: searchForm.value.number,
      }).then((res) => {
        foodTableData.value = res.data.content;
        totalNum.value = res.data.totalElements;
      });
    }
    // 表单重置
    function reset() {
      searchRef.value.resetFields();
      getAuditFoodList();
    }
    // 多选数组
    const multipleArray = ref([]);
    // 多选操作
    function handleSelectionChange(val) {
      multipleArray.value = val;
    }
    // 批量审核
    function MultipleAudit() {
      if (multipleArray.value.length > 0) {
        foodAuditDialogVisible.value = true;
      } else {
        ElMessage.warning("请选择数据后再重试！");
      }
    }
    // 菜品审核对话框
    const foodAuditDialogVisible = ref(false);
    // 审核意见
    const foodAuditContent = ref("");
    // 通过审核
    const ids = ref([]);
    function submitAuit() {
      multipleArray.value.map((item) => {
        ids.value.push(item.id);
      });
      if (foodAuditContent.value) {
        foodMultipleSubmit({
          auditFlag: true,
          auditReason: foodAuditContent.value,
          ids: ids.value.join(","),
        })
          .then(() => {
            ElMessage.success("审核成功");
            foodAuditDialogVisible.value = false;
            foodAuditContent.value = "";
            getAuditFoodList();
          })
          .catch(() => {
            foodAuditDialogVisible.value = false;
            foodAuditContent.value = "";
            getAuditFoodList();
          });
      } else {
        ElMessage.warning("请填写审核意见");
      }
    }
    // 驳回审核
    function rejectAudit() {
      multipleArray.value.map((item) => {
        ids.value.push(item.id);
      });
      if (foodAuditContent.value) {
        foodMultipleSubmit({
          auditFlag: false,
          auditReason: foodAuditContent.value,
          ids: ids.value.join(","),
        })
          .then(() => {
            ElMessage.success("驳回成功");
            foodAuditDialogVisible.value = false;
            foodAuditContent.value = "";
            getAuditFoodList();
          })
          .catch(() => {
            foodAuditDialogVisible.value = false;
            foodAuditContent.value = "";
            getAuditFoodList();
          });
      } else {
        ElMessage.warning("请填写审核意见");
      }
    }
    onMounted(() => {
      getCanteen();
      getAuditFoodList();
    });
    return {
      searchForm,
      canteenOptions,
      pageSize,
      pageNum,
      totalNum,
      foodTableData,
      pageSizeChange,
      pageCurrentChange,
      audit,
      search,
      searchRef,
      reset,
      handleSelectionChange,
      MultipleAudit,
      foodAuditDialogVisible,
      foodAuditContent,
      submitAuit,
      rejectAudit,
    };
  },
};
</script>

<style scoped lang="scss">
.header-search {
  display: flex;
  justify-content: space-between;
  .header-right {
    margin-top: 4px;
  }
}
</style>
